"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.
"""

"""
Case Type   : 资源池化
Case Name   : 资源池化场景下，无任何业务压力, 查询页面分布信息
Create At   : 2024-04-07
Owner       : zhongchengzhi
Description :
    1、查询集群状态
    2、创建段页式表`viewTb`，并向表中插入一条记录
    3、关闭集群
    4、启动集群
    5、恢复集群原始状态
    6、在主节点连接数据库并查询指定页面分布信息
    7、备节点查询表数据
    8、在主节点连接数据库并查询指定页面分布信息
    9、主节点向表中写入数据
    10、在主节点连接数据库并查询指定页面分布信息
    11、删除段页式表`viewTb`
    12、恢复集群原始状态

Expect      :
    1、查询结果正常
    2、创建、插入数据成功
    3、关闭集群成功
    4、启动集群成功，集群状态正常，数据库节点状态正常
    5、恢复集群原始状态成功
    6、集群中没有给定页面的信息
    7、查询结果正常
    8、备机成为页面owner
    9、写入数据正常
    10、主节点成为页面owner，持有X锁，备机不持有页面副本
    11、删除成功
    12、恢复集群原始状态成功,集群状态正常
History     :
    1、2024-04-07 创建
"""
import os
import unittest
import time

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.s1_comsh = CommonSH('Standby1DbUser')

    def test_shared_storage(self):
        text = '-----step1:查询数据库集群状态-----'
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:创建段页式表`viewTb`，并向表中插入一条记录; expect:创建、插入数据成功-----'
        self.log.info(text)
        sql = f"create table viewTb(id int); insert into viewTb values(1);"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('CREATE TABLE\nINSERT 0 1', status, '执行失败' + text)

        text = '-----step3:关闭集群; expect:关闭集群成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn('cm_ctl: stop cluster successfully.', status, '执行失败' + text)

        text = '-----step4:启动集群 expect:启动集群成功，集群状态正常，数据库节点状态正常-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='start')
        self.log.info(status)
        self.assertIn('cm_ctl: start cluster successfully.', status, '执行失败' + text)

        text = '-----step5:启动集群后切主 expect:切主成功，数据库节点状态正常-----'
        self.log.info(text)
        switch = self.s1_comsh.exec_cm_ctl(mode='switchover',
                                           param='-n 2',
                                           path=f'-D {macro.DB_INSTANCE_PATH}')
        self.log.info(switch)
        self.assertIn(self.constant.cm_switchover_success_msg, switch,
                      '执行失败' + text)

        text = '-----step6:在主节点连接数据库并查询指定页面分布信息 expect:集群中没有给定页面信息-----'
        self.log.info(text)
        statu = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(statu)
        sql = f"select * from query_page_distribution_info('viewTb', 0, 0);"
        self.log.info(sql)
        status = self.s1_comsh.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('INFO:  buffer does not exist in local buffer', status, '执行失败' + text)


        text = '-----step7:主节点查询表数据 expect:查询结果正常-----'
        self.log.info(text)
        sql = f"select * from viewTb;"
        self.log.info(sql)
        status = self.s1_comsh.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('id \n----\n  1\n(1 row)', status, '执行失败' + text)
        time.sleep(5)

        text = '-----step8:在主节点连接数据库并查询指定页面分布信息 expect:备机成为页面owner-----'
        self.log.info(text)
        sql = f"select * from query_page_distribution_info('viewTb', 0, 0);"
        self.log.info(sql)
        status = self.s1_comsh.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('(1 row)', status, '执行失败' + text)

        text = '-----step9:主节点向表中写入数据 expect:写入数据正常-----'
        self.log.info(text)
        sql = f"insert into viewTb select * from viewTb;"
        self.log.info(sql)
        status = self.s1_comsh.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('INSERT 0 1', status, '执行失败' + text)

        text = '-----step10:在主节点连接数据库并查询指定页面分布信息 expect:主节点成为页面owner，持有X锁，备机不持有页面副本-----'
        self.log.info(text)
        sql = f"select * from query_page_distribution_info('viewTb', 0, 0);"
        self.log.info(sql)
        status = self.s1_comsh.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('(1 row)', status, '执行失败' + text)

    def tearDown(self):
        text = '-----step11:删除段页式表`viewTb` expect:删除表tb成功-----'
        self.log.info(text)
        sql = f"drop table viewTb;"
        self.log.info(sql)
        status = self.s1_comsh.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('DROP TABLE', status, '执行失败' + text)

        text = '-----step12:恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        status = Primary_SH.check_cluster_core_status(status, wait_times=600)
        self.log.info(status)
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn('cm_ctl: stop cluster successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='start')
        self.log.info(status)
        self.assertIn('cm_ctl: start cluster successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(status)
        self.assertIn(self.constant.cm_switchover_success_msg, status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
